#include "page_manager.h"

#include "epd.h"
#include <stdio.h>



float     Temperature;              // 保存处理后的温度值
float     Humidity;                 // 保存处理后的湿度值
uint8_t   Move_Week;                // 星期显示参数

uint8_t sec;
uint8_t min;

uint8_t home_page_update_flag = 0;

static void Setup(uint8_t condition)
{
	printf("page home setup\n");
#if 1
	// 时间
	epd_1gray_display_part_icon(Num[13/10], 280, 124+48*0, 48, 104);
	epd_1gray_display_part_icon(Num[13%10], 280, 124+48*1, 48,104);
	epd_1gray_display_part_icon(gImage_numdot, 280, 124+48*2, 48,104);
	epd_1gray_display_part_icon(Num[sec/10], 280, 124+48*3, 48,104);
	epd_1gray_display_part_icon(Num[sec%10], 280, 124+48*4, 48,104);			
	// 日期
	epd_1gray_display_part_icon(Date[5/10], 428, 139+13*0, 13,32);  // 428
	epd_1gray_display_part_icon(Date[5%10], 428, 139+13*1, 13,32);
	epd_1gray_display_part_icon(Slash, 429, 139+13*2, 14,32);                    // 429
	epd_1gray_display_part_icon(Date[17/10], 428, 139+13*2+14, 13,32);
	epd_1gray_display_part_icon(Date[17%10], 428, 139+13*3+14, 13,32);                     
	// 星期  
	Move_Week = 7;
	// do
	// {
	// 	Get_Time.week = Get_Time.week>>1;
	// 	Move_Week++;
	// }while(Get_Time.week);						 
	epd_1gray_display_part_icon(gImage_week_chinese[Move_Week-1], 430, 220, 93,32); // 汉字星期 430
	epd_1gray_display_part_icon(gImage_week_logo[Move_Week-1], 426, 317, 34,32); // LOGO  426	

	// 温湿度图标
	epd_1gray_display_part_icon(gImage_Humidity, 380, 139, 32, 32);
	epd_1gray_display_part_icon(gImage_Temperature, 380, 248, 32, 32);
	// 温度数据
	Temperature = Temperature*10;
	epd_1gray_display_part_icon(Tem_Hum_Num[(int16_t)Temperature/100], 388, 166, 16,16); // 388
	epd_1gray_display_part_icon(Tem_Hum_Num[(int16_t)Temperature%100/10], 388, 178, 16,16); // 388
	epd_1gray_display_part_icon(point, 388, 190, 10,16);                // 388
	epd_1gray_display_part_icon(Tem_Hum_Num[(int16_t)Temperature%10], 388, 197, 16,16);  // 388
	epd_1gray_display_part_icon(Temperature_measurement, 388, 211, 16,16);	 // 388				 
	// 湿度数据
	Humidity = Humidity*10;
	epd_1gray_display_part_icon(Tem_Hum_Num[(int16_t)Humidity/100], 388, 280, 16,16); // 388  (uint16_t)Humidity*10/100
	epd_1gray_display_part_icon(Tem_Hum_Num[(int16_t)Humidity%100/10], 388, 292, 16,16); // 388  (uint16_t)Humidity*10%100/10
	epd_1gray_display_part_icon(point, 388, 304, 16,16);               // 388  
	epd_1gray_display_part_icon(Tem_Hum_Num[(int16_t)Humidity%10],388, 313, 16,16); // 388  (uint16_t)Humidity*10%10
	epd_1gray_display_part_icon(Humidity_measurement, 388, 327, 16,16);// 388	
	epd_auto_refresh(0); 
#endif

#if 0
	// 时间
	epd_gd_dis_part_ram(280, 124+48*0, Num[13/10],48,104);
	epd_gd_dis_part_ram(280, 124+48*1, Num[13%10],48,104);
	epd_gd_dis_part_ram(280, 124+48*2, gImage_numdot,48,104);
	epd_gd_dis_part_ram(280, 124+48*3, Num[sec/10],48,104);
	epd_gd_dis_part_ram(280, 124+48*4, Num[sec%10],48,104);				

	// 日期
	epd_gd_dis_part_ram(428, 139+13*0, Date[5/10],13,32);  // 428
	epd_gd_dis_part_ram(428, 139+13*1, Date[5%10],13,32);
	epd_gd_dis_part_ram(429, 139+13*2, Slash,14,32);                    // 429
	epd_gd_dis_part_ram(428, 139+13*2+14, Date[7/10],13,32);
	epd_gd_dis_part_ram(428, 139+13*3+14, Date[7%10],13,32);                     
	// 星期  
	Move_Week = 7;
	// do
	// {
	// 	Get_Time.week = Get_Time.week>>1;
	// 	Move_Week++;
	// }while(Get_Time.week);						 
	epd_gd_dis_part_ram(430, 220, gImage_week_chinese[Move_Week-1],93,32); // 汉字星期 430
	epd_gd_dis_part_ram(426, 317, gImage_week_logo[Move_Week-1],34,32); // LOGO  426

	// 温湿度图标
	epd_gd_dis_part_ram(380, 139, gImage_Temperature, 32, 32);  // 380
	epd_gd_dis_part_ram(380, 248, gImage_Humidity, 32, 32);     // 380			 
	// 温度数据
	Temperature = Temperature*10;
	epd_gd_dis_part_ram(388, 166, Tem_Hum_Num[(int16_t)Temperature/100],16,16); // 388
	epd_gd_dis_part_ram(388, 178, Tem_Hum_Num[(int16_t)Temperature%100/10],16,16); // 388
	epd_gd_dis_part_ram(388, 190, point,10,16);                // 388
	epd_gd_dis_part_ram(388, 197, Tem_Hum_Num[(int16_t)Temperature%10],16,16);  // 388
	epd_gd_dis_part_ram(388, 211, Temperature_measurement,16,16);	 // 388				 
	// 湿度数据
	Humidity = Humidity*10;
	epd_gd_dis_part_ram(388, 280, Tem_Hum_Num[(int16_t)Humidity/100],16,16); // 388  (uint16_t)Humidity*10/100
	epd_gd_dis_part_ram(388, 292, Tem_Hum_Num[(int16_t)Humidity%100/10],16,16); // 388  (uint16_t)Humidity*10%100/10
	epd_gd_dis_part_ram(388, 304, point,16,16);               // 388  
	epd_gd_dis_part_ram(388, 313, Tem_Hum_Num[(int16_t)Humidity%10],16,16); // 388  (uint16_t)Humidity*10%10
	epd_gd_dis_part_ram(388, 327, Humidity_measurement,16,16);// 388	

	epd_gd_part_update();
#endif
	// epd_deep_sleep();
	epd_delay(100);

} 

static void Loop(void)
{
    static uint32_t i = 0;
	static uint32_t j = 0;
	// printf("page home loop %d %d\n",(int)i,(int)j);
	i++;
	if(i > 10)
	{
		i=0;
		j++;
		if(j > 10)
		{
			j = 0;
			sec++;
			printf("sec %d\n",sec);
			if(sec > 59)
				sec = 0;
			home_page_update_flag = 1;

			// epd_1gray_refresh(EPD_REF_MODE_A2);		
		}
	}
	if(home_page_update_flag)
	{
		home_page_update_flag = 0;
#if 1
		// 时间
		epd_1gray_display_part_icon(Num[13/10], 280, 124+48*0, 48, 104);
		epd_1gray_display_part_icon(Num[13%10], 280, 124+48*1, 48,104);
		epd_1gray_display_part_icon(gImage_numdot, 280, 124+48*2, 48,104);
		epd_1gray_display_part_icon(Num[sec/10], 280, 124+48*3, 48,104);
		epd_1gray_display_part_icon(Num[sec%10], 280, 124+48*4, 48,104);			
		// 日期
		epd_1gray_display_part_icon(Date[5/10], 428, 139+13*0, 13,32);  // 428
		epd_1gray_display_part_icon(Date[5%10], 428, 139+13*1, 13,32);
		epd_1gray_display_part_icon(Slash, 429, 139+13*2, 14,32);                    // 429
		epd_1gray_display_part_icon(Date[17/10], 428, 139+13*2+14, 13,32);
		epd_1gray_display_part_icon(Date[17%10], 428, 139+13*3+14, 13,32);                     
		// 星期  
		Move_Week = 7;
		// do
		// {
		// 	Get_Time.week = Get_Time.week>>1;
		// 	Move_Week++;
		// }while(Get_Time.week);						 
		epd_1gray_display_part_icon(gImage_week_chinese[Move_Week-1], 430, 220, 93,32); // 汉字星期 430
		epd_1gray_display_part_icon(gImage_week_logo[Move_Week-1], 426, 317, 34,32); // LOGO  426	

		// 温湿度图标
		epd_1gray_display_part_icon(gImage_Humidity, 380, 139, 32, 32);
		epd_1gray_display_part_icon(gImage_Temperature, 380, 248, 32, 32);
		// 温度数据
		Temperature = Temperature*10;
		epd_1gray_display_part_icon(Tem_Hum_Num[(int16_t)Temperature/100], 388, 166, 16,16); // 388
		epd_1gray_display_part_icon(Tem_Hum_Num[(int16_t)Temperature%100/10], 388, 178, 16,16); // 388
		epd_1gray_display_part_icon(point, 388, 190, 10,16);                // 388
		epd_1gray_display_part_icon(Tem_Hum_Num[(int16_t)Temperature%10], 388, 197, 16,16);  // 388
		epd_1gray_display_part_icon(Temperature_measurement, 388, 211, 16,16);	 // 388				 
		// 湿度数据
		Humidity = Humidity*10;
		epd_1gray_display_part_icon(Tem_Hum_Num[(int16_t)Humidity/100], 388, 280, 16,16); // 388  (uint16_t)Humidity*10/100
		epd_1gray_display_part_icon(Tem_Hum_Num[(int16_t)Humidity%100/10], 388, 292, 16,16); // 388  (uint16_t)Humidity*10%100/10
		epd_1gray_display_part_icon(point, 388, 304, 16,16);               // 388  
		epd_1gray_display_part_icon(Tem_Hum_Num[(int16_t)Humidity%10],388, 313, 16,16); // 388  (uint16_t)Humidity*10%10
		epd_1gray_display_part_icon(Humidity_measurement, 388, 327, 16,16);// 388	
		epd_auto_refresh(1); 
#endif
#if 0
		// 时间
		epd_gd_dis_part_ram(280, 124+48*0, Num[13/10],48,104);
		epd_gd_dis_part_ram(280, 124+48*1, Num[13%10],48,104);
		epd_gd_dis_part_ram(280, 124+48*2, gImage_numdot,48,104);
		epd_gd_dis_part_ram(280, 124+48*3, Num[sec/10],48,104);
		epd_gd_dis_part_ram(280, 124+48*4, Num[sec%10],48,104);				

		// 日期
		epd_gd_dis_part_ram(428, 139+13*0, Date[5/10],13,32);  // 428
		epd_gd_dis_part_ram(428, 139+13*1, Date[5%10],13,32);
		epd_gd_dis_part_ram(429, 139+13*2, Slash,14,32);                    // 429
		epd_gd_dis_part_ram(428, 139+13*2+14, Date[7/10],13,32);
		epd_gd_dis_part_ram(428, 139+13*3+14, Date[7%10],13,32);                     
		// 星期  
		Move_Week = 7;
		// do
		// {
		// 	Get_Time.week = Get_Time.week>>1;
		// 	Move_Week++;
		// }while(Get_Time.week);						 
		epd_gd_dis_part_ram(430, 220, gImage_week_chinese[Move_Week-1],93,32); // 汉字星期 430
		epd_gd_dis_part_ram(426, 317, gImage_week_logo[Move_Week-1],34,32); // LOGO  426

		// 温湿度图标
		epd_gd_dis_part_ram(380, 139, gImage_Temperature, 32, 32);  // 380
		epd_gd_dis_part_ram(380, 248, gImage_Humidity, 32, 32);     // 380			 
		// 温度数据
		Temperature = Temperature*10;
		epd_gd_dis_part_ram(388, 166, Tem_Hum_Num[(int16_t)Temperature/100],16,16); // 388
		epd_gd_dis_part_ram(388, 178, Tem_Hum_Num[(int16_t)Temperature%100/10],16,16); // 388
		epd_gd_dis_part_ram(388, 190, point,10,16);                // 388
		epd_gd_dis_part_ram(388, 197, Tem_Hum_Num[(int16_t)Temperature%10],16,16);  // 388
		epd_gd_dis_part_ram(388, 211, Temperature_measurement,16,16);	 // 388				 
		// 湿度数据
		Humidity = Humidity*10;
		epd_gd_dis_part_ram(388, 280, Tem_Hum_Num[(int16_t)Humidity/100],16,16); // 388  (uint16_t)Humidity*10/100
		epd_gd_dis_part_ram(388, 292, Tem_Hum_Num[(int16_t)Humidity%100/10],16,16); // 388  (uint16_t)Humidity*10%100/10
		epd_gd_dis_part_ram(388, 304, point,16,16);               // 388  
		epd_gd_dis_part_ram(388, 313, Tem_Hum_Num[(int16_t)Humidity%10],16,16); // 388  (uint16_t)Humidity*10%10
		epd_gd_dis_part_ram(388, 327, Humidity_measurement,16,16);// 388	

		epd_gd_part_update();
#endif
	}

} 

static void Exit(uint8_t condition)
{
	printf("page_home Exit\n");
	epd_1gray_init();
	// epd_gd_writescreen_white(); // 清屏
	

}

static void Event(uint8_t event)
{
	printf("page_menu Event: %d\n", event);
	if(event == 1 || event == 2 || event == 3)
	{
		page_manager_switch_page(page_id_menu, PAGE_CONDITION_ENTER);
	}
}




// 注册页面
void page_register_page_id_home(uint8_t page_id)
{
	page_register(page_id, NULL, NULL, Setup, Loop, Exit, Event);
}







